/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.user.rest;

import co.yixiang.api.ApiResult;
import co.yixiang.enums.BillDetailEnum;
import co.yixiang.enums.BillEnum;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.shop.rest.CommissionNewController;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.service.YxUserBillService;
import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.modules.user.service.dto.YxUserBillQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
* @author hupeng
* @date 2019-11-06
*/
@Api(tags = "商城:用户账单管理")
@RestController
@RequestMapping("api")
public class UserBillController {

    private final YxUserBillService yxUserBillService;
    private final YxUserService yxUserService;
    private final co.yixiang.modules.shop.rest.CommissionNewController CommissionNewController;
    //private final TDMQMessageController tdmqMessage;

    public UserBillController(YxUserBillService yxUserBillService, YxUserService yxUserService, co.yixiang.modules.shop.rest.CommissionNewController commissionNewController) {
        this.yxUserBillService = yxUserBillService;
        this.yxUserService = yxUserService;
        CommissionNewController = commissionNewController;
    }

    @Log("查询")
    @ApiOperation(value = "查询")
    @GetMapping(value = "/yxUserBill")
    @PreAuthorize("hasAnyRole('admin','YXUSERBILL_ALL','YXUSERBILL_SELECT')")
    public ResponseEntity getYxUserBills(YxUserBillQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxUserBillService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("公司转账积分")
    @ApiOperation(value = "公司转账积分")
    @PostMapping(value = "/yxUserBill/trader/{uid}/{traderSum}/{lableName}")
    @PreAuthorize("hasAnyRole('admin','YXUSERBILL_ALL','YXUSERBILL_SELECT')")
    public ApiResult<String> trader( @PathVariable String uid, @PathVariable String traderSum, @PathVariable String lableName){
        YxUser toUser =yxUserService.getById(Long.parseLong(uid));
        BigDecimal tSum =new BigDecimal(traderSum);
        this.traderMoney(toUser,tSum,lableName);
        return ApiResult.ok("","转账成功");
    }

    @Log("重新生成分润")
    @ApiOperation(value = "重新生成分润")
    @PostMapping(value = "/yxUserBill/initCommission/{orderId}")
    public void initCommission( @PathVariable String orderId){
        CommissionNewController.checkOrderGoodsclass(orderId);
        //return ApiResult.ok("","转账成功");
    }

    public void traderMoney(YxUser toUser, BigDecimal traderSum,String lableName)
    {
        //扣减当前用户余额
        BigDecimal toUsertotalSum =toUser.getNowMoney().add(traderSum);
        BigDecimal fromUsertotalSum = new BigDecimal("0");
        yxUserBillService.expend4traderCommission(new Long(0),toUser.getUid(),"18", BillDetailEnum.CATEGORY_1.getValue(),
                BillDetailEnum.TYPE_3.getValue(),traderSum.doubleValue(),fromUsertotalSum.doubleValue(), lableName,toUser.getPhone(), BillEnum.PM_0.getValue());
//增加目标用户余额
        yxUserBillService.expend4traderCommission(toUser.getUid(),new Long(0),"18", BillDetailEnum.CATEGORY_1.getValue(),
                BillDetailEnum.TYPE_2.getValue(),traderSum.doubleValue(),toUsertotalSum.doubleValue(), lableName,lableName, BillEnum.PM_1.getValue());
        yxUserService.incMoney(toUser.getUid(),traderSum);
        yxUserService.incBrokeragePrice(traderSum,toUser.getUid());
    }

}
